/**
 * Copyright &copy; com.smooth.common
 */
package com.smooth.site.sx.mag.biz.offer.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.smooth.common.site.common.config.Global;
import com.smooth.common.site.common.excep.SiteException;
import com.smooth.common.site.common.persistence.Page;
import com.smooth.common.site.common.utils.StringUtils;
import com.smooth.common.site.common.web.BaseController;
import com.smooth.dao.gen.bean.BizOffer;
import com.smooth.site.sx.mag.biz.offer.service.BizOfferService;

/**
 * offerController
 * @author bm
 * @version 2019-02-25
 */
@Controller
@RequestMapping(value = "${adminPath}/offer/bizOffer")
public class BizOfferController extends BaseController {

	@Autowired
	private BizOfferService bizOfferService;
	
	@ModelAttribute
	public BizOffer get(@RequestParam(required=false) String id,@RequestParam(required=false) String opt,HttpServletRequest request) {
		BizOffer entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = bizOfferService.get(id);
		}
		if (StringUtils.isNotBlank(opt)){
			request.setAttribute("opt", opt);
		}
		if (entity == null){
			entity = new BizOffer();
		}
		return entity;
	}
	
	@RequiresPermissions("offer:bizOffer:view")
	@RequestMapping(value = {"list", ""})
	public String list(BizOffer bizOffer, HttpServletRequest request, HttpServletResponse response, Model model) {
		try{
			Page<BizOffer> page = bizOfferService.findPage(new Page<BizOffer>(request, response), bizOffer); 
			model.addAttribute("page", page);
		}catch(SiteException e){
			model.addAttribute("message", e.getMessage());
			return "biz/offer/bizOfferList";
		}
		return "biz/offer/bizOfferList";
	}
	
	@RequiresPermissions("offer:bizOffer:view")
	@RequestMapping(value = "form")
	public String form(BizOffer bizOffer, Model model) {
		model.addAttribute("bizOffer", bizOffer);
		return "biz/offer/bizOfferForm";
	}

	@RequiresPermissions("offer:bizOffer:edit")
	@RequestMapping(value = "save")
	public String save(BizOffer bizOffer, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, bizOffer)){
			return form(bizOffer, model);
		}
		try{
			bizOfferService.save(bizOffer);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "redirect:"+Global.getAdminPath()+"/offer/bizOffer?repage";
		}
		addMessage(redirectAttributes, "保存offer成功");
		return "redirect:"+Global.getAdminPath()+"/offer/bizOffer?repage";
	}
	
	@RequiresPermissions("offer:bizOffer:edit")
	@RequestMapping(value = "delete")
	public String delete(BizOffer bizOffer, RedirectAttributes redirectAttributes) {
		try{
			bizOfferService.delete(bizOffer);
		}catch(SiteException e){
			addMessage(redirectAttributes, e.getMessage());
			return "redirect:"+Global.getAdminPath()+"/offer/bizOffer?repage";
		}
		addMessage(redirectAttributes, "删除offer成功");
		return "redirect:"+Global.getAdminPath()+"/offer/bizOffer?repage";
	}

}